package csloader.loaders
{
	import com.adobe.serialization.json.JSON;
	import com.adobe.serialization.json.JSONParseError;
	
	import flash.events.Event;
	
	import csloader.events.ExtraEvent;

	public class JSONLoader extends TextLoaderBase
	{
		public function JSONLoader(callback:Function)
		{
			super(callback);
		}
		
		/**
		 * 验证返回的字符串是否为有效的JSON对象
		 * @param url
		 * @param response
		 * 
		 */
		override public function sendResponse(url:String, response:*=null) : void
		{
			//响应错误，返回的数据不是JSON对象
			try	{
				response = JSON.decode(response);
			} catch(error:JSONParseError) {
				callback.apply(null, [new ExtraEvent(Event.COMPLETE, url)]);
				trace("与 " + url + " 通讯时发生响应错误，错误代码：无效的JSON对象。");
				return;
			}
			//发送响应成功事件
			super.sendResponse(url, response);
		}
	}
}